<?php if ( ! defined('BASEPATH')){ exit('No direct script access allowed');}

class polos extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('polos_model');
        allow_admin_only($this->session->userdata['user_role']);
    }
    
    public function index()
    {
        $polos_list=$this->polos_model->get_polos();
        $dados = array(
                //carrega views
                'header' => 'header.php',
                'divheader' => 'divheader.php',
                'divcontent' => 'divcontent_polos.php',
                'divleftmenu' => 'divleftmenu.php',
                'divfooter' => 'divfooter.php',
                'polos_list' => $polos_list,
                'perfil' => retornaperfil($this->session->userdata('user_role')),
                'menu_item' => criamenu($this->session->userdata('id'),$this->session->userdata('user_role')),
            );
        $this->load->view('main', $dados);
       
    }
    
    public function polo_edit(){
        
        $this->form_validation->set_rules('nome','NOME','required|max_lenght[254]');
        $this->form_validation->set_rules('cidade','CIDADE','required|max_lenght[254]');
        $this->form_validation->set_rules('endereco','ENDEREÇO','required|max_lenght[254]');
        $this->form_validation->set_rules('bairro','BAIRRO','required|max_lenght[254]');
        $this->form_validation->set_rules('estado','ESTADO','required|max_lenght[254]');
        $this->form_validation->set_rules('cep','CEP','required|max_lenght[9]');
        $this->form_validation->set_rules('dd_tel','DDD','required|max_lenght[2]');
        $this->form_validation->set_rules('tel','TELEFONE','required|max_lenght[8]');
        
        if($this->form_validation->run()==TRUE){
             $dados_polo= elements(array('nome','cidade','endereco','bairro',
            'estado','cep','dd_tel','tel'),$this->input->post());
        $this->polos_model->update_polo($dados_polo,array('id'=>$this->uri->segment(3)));
        }
        
        $dados_polo=$this->polos_model->get_polo_info($this->uri->segment(3));
        $dados = array(
                //carrega views
                'header' => 'header.php',
                'divheader' => 'divheader.php',
                'divcontent' => 'divcontent_polos_edit.php',
                'divleftmenu' => 'divleftmenu.php',
                'divfooter' => 'divfooter.php',
                'dados_polo' => $dados_polo,
                'perfil' => retornaperfil($this->session->userdata('user_role')),
                'menu_item' => criamenu($this->session->userdata('id'),$this->session->userdata('user_role')),
            );
        $this->load->view('main', $dados);
    }
    
    public function novo_polo(){
        
        $this->form_validation->set_rules('nome','NOME','required|max_lenght[254]');
        $this->form_validation->set_rules('cidade','CIDADE','required|max_lenght[254]');
        $this->form_validation->set_rules('endereco','ENDEREÇO','required|max_lenght[254]');
        $this->form_validation->set_rules('bairro','BAIRRO','required|max_lenght[254]');
        $this->form_validation->set_rules('estado','ESTADO','required|max_lenght[254]');
        $this->form_validation->set_rules('cep','CEP','required|max_lenght[9]');
        $this->form_validation->set_rules('dd_tel','DDD','required|max_lenght[2]');
        $this->form_validation->set_rules('tel','TELEFONE','required|max_lenght[8]');
        
        if($this->form_validation->run()==TRUE){
            $dados_polo= elements(array('nome','cidade','endereco','bairro','complemento',
            'estado','cep','dd_tel','tel'),$this->input->post());
            $dados_polo['id']=$this->polos_model->get_last_polo_id();
            $this->polos_model->novo_polo($dados_polo);
        }
        
        $dados = array(
                //carrega views
                'header' => 'header.php',
                'divheader' => 'divheader.php',
                'divcontent' => 'divcontent_polos_novo.php',
                'divleftmenu' => 'divleftmenu.php',
                'divfooter' => 'divfooter.php',
                'perfil' => retornaperfil($this->session->userdata('user_role')),
                'menu_item' => criamenu($this->session->userdata('id'),$this->session->userdata('user_role')),
            );
        $this->load->view('main', $dados);
    }
}

/* End of file main.php */
/* Location: ./application/controllers/main.php */